
package com.jf.cloud.service.sysmanager.dao.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.service.spi.ServiceException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.jf.cloud.service.sysmanager.common.base.BaseService;
import com.jf.cloud.service.sysmanager.common.base.GenericsUtils;
import com.jf.cloud.service.sysmanager.common.db.BaseDao;
import com.jf.cloud.service.sysmanager.common.db.Condition;
import com.jf.cloud.service.sysmanager.common.util.ObjectUtil;
import com.jf.cloud.service.sysmanager.dao.IPubgBasePropsConfigService;
import com.jf.cloud.service.sysmanager.entity.PubgBasePropsConfig;
import com.jf.cloud.service.sysmanager.handler.PropConfigHandler;


/**
 * 员工服务实现
 * 
 * @author 绝峰代码机1.0 创建时间：2018-03-31
 */
@Service("pubgBasePropsConfigService")
public class PubgBasePropsConfigServiceImpl extends BaseService<PubgBasePropsConfig, Long> implements IPubgBasePropsConfigService {

	/**
	 * 日志
	 */
	private static Log log = LogFactory.getLog(PubgBasePropsConfigServiceImpl.class);

	/**
	 * JPA公共DAO
	 */
	@Resource
	private BaseDao baseDao;

	private RowMapper<PubgBasePropsConfig> rowMapper = new BeanPropertyRowMapper<PubgBasePropsConfig>(GenericsUtils.getSuperClassGenricType(this.getClass()));

	/**
	 * 根据主键查询员工
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public PubgBasePropsConfig findPubgBasePropsConfig(Long rowId) throws Exception {
		try {
			return this.baseDao.findObject("", rowMapper, rowId);
		} catch (Exception e) {
			throw new Exception("根据主键查询员工出错！", e);
		}
	}

	/**
	 * 根据查询条件获取员工列表
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	public List<PubgBasePropsConfig> findPubgBasePropsConfigList(Condition qc) throws Exception {
		try {
			return this.baseDao.findList("", rowMapper);
		} catch (Exception e) {
			throw new Exception("根据查询条件获取员工列表出错！", e);
		}
	}

	/**
	 * 新增/修改员工
	 * 
	 * @author 绝峰代码机1.0 创建时间：2018-03-31
	 */
	@Transactional
	public PubgBasePropsConfig saveOrUpdatePubgBasePropsConfig(PubgBasePropsConfig pubgBasePropsConfig) throws Exception {
		try {
			return this.baseDao.saveAll(pubgBasePropsConfig);
		} catch (Exception e) {
			throw new Exception("新增/修改员工出错！", e);
		}
	}

	/**
	 * 刷新基础属性配置信息至常量
	 * 
	 * @author 朱建文 创建时间：2018年4月3日 上午11:06:19
	 */
	public void refreshConfigToContant() throws Exception {
		Map<String, Object> cfgData = new HashMap<String, Object>();
		try {
			String sql = " SELECT CFG_KEY, CFG_VALUE FROM pubg_base_props_config WHERE DEL_FLAG = '1' ";
			List<Map<String, Object>> dataList = this.baseDao.findMapList(sql);
			if (dataList != null && !dataList.isEmpty()) {
				for (Map<String, Object> data : dataList) {
					cfgData.put(ObjectUtil.convert(data.get("CFG_KEY")), data.get("CFG_VALUE"));
				}				
			}
			PropConfigHandler.setBasePropConfig(cfgData);
			
			if (PropConfigHandler.getBasePropConfig().containsKey("is_reset_flag")) {
				if ("10".equals(PropConfigHandler.getBasePropConfig("is_reset_flag"))) {
					PropConfigHandler.setBasePropConfig("is_reset_flag", "0");
				}
			}
		} catch (Exception e) {
			throw new ServiceException("刷新基础属性配置信息至常量出错！", e);
		}
	}
}
